package com.xiaobo.client;

import android.app.Activity;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;

public class LocationDw extends Activity implements LocationListener {
	private static final String TAG = "LocationDw";

	private LocationManager locationManager;
	private String bestProvider;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// output = (TextView) findViewById(R.id.output);
		// Get the location manager
 

		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		// List all providers:

		Criteria criteria = new Criteria();
		bestProvider = locationManager.getBestProvider(criteria, false);
		Location location = locationManager.getLastKnownLocation(bestProvider);
		printLocation(location);
	}

	@Override
	protected void onResume() {
		super.onResume();
		locationManager.requestLocationUpdates(bestProvider, 200, 1, this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		locationManager.removeUpdates(this);
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		printLocation(location);
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	private void printLocation(Location location) {
		if (location == null){
			Toast toast = Toast.makeText(getApplicationContext(),
					"自定义位置location：Location[unknown]", Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		}
		else{
			Toast toast = Toast.makeText(getApplicationContext(),
					"自定义位置location：" + location.toString(), Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			
		}
	}
}
